﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

 [Serializable]
public partial class Admin_panel_CauHinhThongKe : System.Web.UI.Page
{

     BieumauDataContext _db = new BieumauDataContext();
  

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["login_admin"]== null)
        {
            Response.Redirect("Index.aspx");
        }
        try
        {

            if (!IsPostBack)
            {
                tblCauHinhThongKe tk = _db.tblCauHinhThongKes.Single(p => p.ID == "001");
                txtDonViGui.Text = tk.DonViGui;
                txtDonViNhan.Text = tk.DonViNhan;
                txtGiamDoc.Text = tk.GiamDoc;
                txtNguoiLapThongKe.Text = tk.NguoiLapBieu;
                txtNoiGui.Text = tk.NoiGui;
           }
             
            
        }
        catch (Exception)
        {
            
            
        }
        
     
       
    }

    protected void btnLuu_Click(object sender, EventArgs e)
    {
        if (txtDonViGui.Text == "" || txtDonViNhan.Text == "" || txtGiamDoc.Text == "" || txtNguoiLapThongKe.Text == "" || txtNoiGui.Text == "")
        {
            lblstatus.Text = "";
        }
        if (Page.IsValid)
        {
            try
            {
               
                    tblCauHinhThongKe tk2 = new tblCauHinhThongKe();
                    tk2.ID = "001";
                    tk2.DonViGui = txtDonViGui.Text;
                    tk2.DonViNhan = txtDonViNhan.Text;
                    tk2.GiamDoc = txtGiamDoc.Text;
                    tk2.NguoiLapBieu = txtNguoiLapThongKe.Text;
                    tk2.NoiGui = txtNoiGui.Text;

                    if (_db.tblCauHinhThongKes.Contains(tk2))
                        throw new Exception("");

                    _db.tblCauHinhThongKes.InsertOnSubmit(tk2);
                    _db.SubmitChanges();
                    lblstatus.ForeColor = System.Drawing.Color.Green;
                    lblstatus.Text = "Thêm thông tin cấu hình thống kê thành công";
                

            }
            catch (Exception)
            {
               
                    tblCauHinhThongKe tk3 = _db.tblCauHinhThongKes.Single(p => p.ID == "001");
                    tk3.DonViGui = txtDonViGui.Text;
                    tk3.DonViNhan = txtDonViNhan.Text;
                    tk3.GiamDoc = txtGiamDoc.Text;
                    tk3.NguoiLapBieu = txtNguoiLapThongKe.Text;
                    tk3.NoiGui = txtNoiGui.Text;

                    _db.SubmitChanges();
                    lblstatus.ForeColor = System.Drawing.Color.Green;
                    lblstatus.Text = "<span class='input-notification success png_bg'></span>Cập nhật thông tin cấu hình thống kê thành công";
                

            }
        }
    }

  
}